package com.lw.chat.view.chat;

import cn.hutool.core.util.StrUtil;
import com.lw.chat.model.ChatModel;
import com.lw.chat.model.Message;
import de.saxsys.mvvmfx.FluentViewLoader;
import de.saxsys.mvvmfx.FxmlView;
import de.saxsys.mvvmfx.InjectViewModel;
import de.saxsys.mvvmfx.ViewTuple;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.Stack;

public class ChatView implements FxmlView<ChatViewModel>, Initializable {
    @InjectViewModel
    private ChatViewModel chatViewModel;

    @FXML
    private ListView<ChatModel> chatListView;
    @FXML
    private VBox msgPane;
    @FXML
    private ScrollPane msgScp;

    @FXML
    private Label title;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

        msgPane.minWidthProperty().bind(Bindings.createDoubleBinding(
                () -> msgScp.widthProperty().getValue()-20, msgScp.widthProperty())
        );
        title.textProperty().bind(chatViewModel.nameProperty());
        Bindings.bindContent(msgPane.getChildren(), chatViewModel.getMessageDataList());
        chatListView.setItems(chatViewModel.getChatDataList());
        chatListView.setFixedCellSize(70);
        chatListView.setCellFactory(listView -> new ListCell<ChatModel>() {
            @Override
            protected void updateItem(ChatModel item, boolean empty) {
                super.updateItem(item, empty);
                if (empty) {
                    setText(null);
                    setGraphic(null);
                } else {
                    ImageView imageView = new ImageView(item.getAvatarUri());
                    imageView.setFitHeight(36);
                    imageView.setFitWidth(36);
                    Label label = new Label(item.getName());
                    label.setMaxWidth(Double.MAX_VALUE);
                    Label time = new Label(item.getLastTime());
                    Label msg = new Label(item.getLastMessage());

                    HBox topBox = new HBox();
                    topBox.getChildren().addAll(label, time);
                    HBox.setHgrow(label, Priority.ALWAYS);

                    HBox buttonBox = new HBox();
                    buttonBox.getChildren().addAll(msg);
                    HBox.setHgrow(buttonBox, Priority.ALWAYS);

                    HBox box = new HBox();
                    box.setAlignment(Pos.CENTER_LEFT);
                    box.setSpacing(7);
                    VBox vBox = new VBox();
                    vBox.setSpacing(5);
                    vBox.getChildren().addAll(topBox, buttonBox);
                    vBox.setAlignment(Pos.CENTER_LEFT);
                    HBox.setHgrow(vBox, Priority.ALWAYS);
                    box.getChildren().addAll(imageView, vBox);
//                    box.setStyle("-fx-background-color: red");
                    setGraphic(box);
                }
            }
        });

        chatListView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            chatViewModel.setChatModel(newValue);

        });



    }
}
